// pages/proInfo/proInfo.js
var ports = require("../../utils/ports.js")
var utils = require("../../utils/util.js");
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    indcolor: '#FFFFFF',
    indactcolor: '#6B162B',
    indicatorDots: true,
    vertical: false,
    autoplay: false,
    interval: 2000,
    duration: 500,
    kfhide: true,
    stock: 100,
    submittext: false,
    showpromodel: false,
    goodsShopID: '',
    featureItemList: [],
    fitemid: '',
    fitemName: '',
    gshowPrice: 0,//当前商品价格
    orderFare: 0,//订单运费
    orderAmountFreeFare: '',//包邮信息
    cartNum: 0,
    ObjectDiscussList: [],//评论    
    likesListNp: [],//猜你喜欢
    likesListOp: [],
    collected: false,//收藏
    bonusDefineList: [],//红包列表
    isinfoflag: false
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      thisBotH: '0'
    })
    var id = options.id;
    var memberID = wx.getStorageSync('USER_MEMBERID')
    if (id) {
      that.setData({
        goodsShopID: id,
        thisgoodsShopID: id
      })
      if (memberID != null) {
        that.getGoodsShopDetail(memberID)
      } else {
        wx.showToast({
          title: '请登录',
          icon: 'none'
        })
      }
    } else {
      wx.showToast({
        title: '参数异常',
        icon: 'none'
      })
      setTimeout(function () {
        wx.navigateBack()
      }, 1000)
    }
    that.getCanGetBonusDefineList()
  },
  //加入购物车，立即购买动画
  setSubmitText: function (e) {
    var that = this
    that.setData({
      submittext: e.currentTarget.dataset.todo == '1' ? true : false,
      amount: 1
    })
    that.setData({
      goodsShopID: e.currentTarget.dataset.id
    })
    var memberID = wx.getStorageSync('USER_MEMBERID')
    that.getGoodsShopDetail(memberID)
    that.setData({
      isinfoflag: true
    })
  },
  //唤起客服弹窗
  kefuto: function () {
    this.setData({
      kfhide: false
    });
  },
  //客服弹窗 呼叫
  kfcall: function () {
    wx.makePhoneCall({
      phoneNumber: '18080628570'
    })
  },
  //客服弹窗 取消
  kfcancle: function () {
    this.setData({
      kfhide: true
    });
  },
  //首页
  toindex: function () {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  //隐藏
  hide_black: function (e) {
    this.setData({
      toggleGoodsFeature: false
    })
  },
  //商品详情
  getGoodsShopDetail: function (memberID) {
    var that = this
    //店铺商品详情
    ports.ModuleGoodsShop.getGoodsShopDetail({
      goodsShopID: that.data.goodsShopID,
      type: 2,
      memberID: memberID
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          goodsShop: res.data.body.data,
          gshowPrice: res.data.body.data.realPrice
        })
        if (that.data.thisgoodsShopID === res.data.body.data.id) {
          that.setData({
            myGoodsShop: res.data.body.data
          })
        }
        if (res.data.body.data.isCollect == 0) {
          that.setData({
            collected: false
          })
        } else {
          that.setData({
            collected: true
          })
        }
        that.getGoodsShopList(res.data.body.data.goodsCategoryID)
        that.getObjectAttachmentList(that.data.myGoodsShop.id)
        that.getFeatureList(that.data.myGoodsShop.id);
        that.getMemberCartGoodsNum(function (res) {
          that.setData({
            cartNum: res.data.body.num
          })
          that.getObjectDiscussList(that.data.myGoodsShop.id)
        })
      }
    })

    //获取订单运费
    ports.ModuleGoodsShop.getParameterValueByCode({
      code: app.orderFare,
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          orderFare: res.data.body.data
        })
      }
    })

    //获取包邮信息
    ports.ModuleGoodsShop.getParameterValueByCode({
      code: app.orderAmountFreeFare,
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          orderAmountFreeFare: res.data.body.data
        })
      }
    })
  },
  //获取商品规格
  getFeatureList(objectID) {
    var that = this
    //获取产品规格   
    ports.ModuleGoodsShop.getObjectFeatureItemList({
      objectID: objectID,
      pageNumber: 999
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          featureItemList: res.data.body.data
        })
        if (that.data.featureItemList.length > 0) {
          that.setData({
            //默认选中第一个规格
            fitemName: that.data.featureItemList[0].itemList[0].objectFeatureItemName,
            fitemid: that.data.featureItemList[0].itemList[0].objectFeatureItemID
          })
        }
        if (that.data.isinfoflag) {
          that.setData({
            toggleGoodsFeature: true,
            showpromodel: true
          })
        }
      }
    })
  },
  //点击规格
  featureItemChoose: function (e) {
    var that = this
    var thisid = e.currentTarget.dataset.id
    that.getObjectFeatureData(that.properties.goodsShop.id, thisid)
    that.setData({
      fitemid: thisid,
      fitemName: e.currentTarget.dataset.fname
    })
  },
  //获取规格item
  getObjectFeatureData(objectID, objectFeatureItemID) {
    var that = this
    //获取产品规格
    ports.ModuleGoodsShop.getObjectFeatureData({
      objectID: objectID,
      objectFeatureItemID1: objectFeatureItemID
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          gmemberPrice: res.data.body.data.memberPrice,
          gshowPrice: res.data.body.data.showPrice
        })
      }
    })
  },
  //猜你喜欢
  getGoodsShopList(categoryID) {
    var that = this
    ports.ModuleGoodsShop.getGoodsShopList({
      goodsCategoryID: categoryID,
      shopID: app.shopID,
      pageNumber: 5,
      keyWords: that.data.keywords ? that.data.keywords : ''
    }, that, res => {
      if (res.data.header.code == 0) {
        var likesList = res.data.body.data.rows;
        that.setData({
          likesListNp: likesList[0],
          likesListOp: likesList.slice(1, likesList.length)
        })
      }
    })
  },
  //猜你喜欢 商品详情
  toGoodsDetail(e) {
    wx.navigateTo({
      url: '/pages/proInfo/proInfo?id=' + e.currentTarget.dataset.id,
    })
  },
  //详情附件记录
  getObjectAttachmentList(objectID) {
    var that = this
    //附件
    ports.ModuleAttachment.getObjectAttachmentList({
      objectID: objectID,
      pageNumber: 999
    }, that, res => {
      if (res.data.header.code == 0) {
        that.setData({
          attachmentList: res.data.body.data.rows
        })
      }
    })
  },
  //评论记录
  getObjectDiscussList(objectID) {
    var that = this
    var sessionID = wx.getStorageSync('USER_SESSIONID')
    if (sessionID) {
      //评论
      ports.ModuleDiscuss.getObjectDiscussList({
        objectID: objectID,
        pageNumber: 2,
        sortTypeTime: 1
      }, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            ObjectDiscussList: res.data.body.data.rows
          })
        }
      })
    }
  },
  //购物车数量
  getMemberCartGoodsNum(fun) {
    var that = this
    var sessionID = wx.getStorageSync('USER_SESSIONID')
    if (sessionID) {
      //评论
      ports.ModuleCart.getMemberCartGoodsNum({}, that, res => {
        if (res.data.header.code == 0) {
          fun(res)
        }
      })
    }
  },
  //加入收藏
  addCollect() {
    var that = this
    var sessionID = wx.getStorageSync('USER_SESSIONID')
    if (sessionID) {
      wx.showToast({
        title: '正在获取权限',
        icon: 'loading',
        mask: true
      })
      var data = {}
      data.sessionID = sessionID
      data.objectID = that.data.goodsShopID
      data.objectName = that.data.goodsShop.name
      data.objectDefineID = app.goodsobjectDefineID
      //加入收藏      
      ports.ModuleStory.addCollect(data, that, res => {
        wx.hideToast()
        if (res.data.header.code == 0) {
          that.setData({
            collected: true
          })
          wx.showToast({
            title: "已收藏",
            icon: 'none'
          })
        } else {
          wx.showToast({
            title: res.data.header.msg,
            icon: 'none'
          })
        }
      })
    } else {
      wx.getUserProfile({
        desc: '用于完善会员资料',
        success: (res) => {
          if (res.userInfo != null) {
            wx.setStorageSync("USER_DEFAULT", {
              avatarUrl: res.userInfo.avatarUrl,
              nickName: res.userInfo.nickName
            })
            utils.doLoginOther()
          }
        }
      })
    }
  },
  //获取红包
  getCanGetBonusDefineList() {
    var that = this
    var sessionID = wx.getStorageSync('USER_SESSIONID')
    if (sessionID) {
      ports.ModuleBonus.getCanGetBonusDefineList({}, that, res => {
        if (res.data.header.code == 0) {
          that.setData({
            bonusDefineList: res.data.body.data.rows
          })
        }
      })
    }
  },
  //领取红包
  getBonus(e) {
    var that = this
    var sessionID = wx.getStorageSync('USER_SESSIONID')
    var bonusDefineID = e.currentTarget.dataset.id
    var myStatus = e.currentTarget.dataset.status
    var canGetNum = e.currentTarget.dataset.cangetnum
    if (sessionID) {
      if (myStatus == 0) {
        if (canGetNum > 0) {
          ports.ModuleBonus.memberGetBonus({
            bonusDefineID: bonusDefineID
          }, that, res => {
            if (res.data.header.code == 0) {
              wx.showToast({
                title: "领取成功"
              })
            } else {
              wx.showToast({
                title: res.data.body.msg,
                icon: 'none'
              })
            }
          })
        } else {
          wx.showToast({
            title: "领取次数用完啦！",
            icon: 'none'
          })
        }
      } else {
        wx.showToast({
          title: "不可重复领取！",
          icon: 'none'
        })
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var memberID = wx.getStorageSync("USER_MEMBERID")
    var id = that.data.goodsShopID;
    return {
      title: app.defaultShareTitle,
      path: `/pages/proInfo/proInfo?memberID=${memberID}&id=${id}`
    }
  }
})
